package com.xingkeduo.service.api.common.impl;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xingkeduo.configuration.RedisMapCacheService;
import com.xingkeduo.service.api.common.CommonService;
import com.xingkeduo.util.RandomUtil;

@Service
public class CommonSreviceImpl implements CommonService {
	Logger logger =LoggerFactory.getLogger(CommonSreviceImpl.class);
	@Autowired
	RedisMapCacheService redisMapCacheService;

	@Override
	public String getOnlyRandomNo(String key,String joinStr) {
		String randomStr="";
		Boolean flag = Boolean.FALSE;
		Long time = (long) (24*60*60*1000);
		while(!flag) {
		    randomStr = RandomUtil.random14UUID();
			if(StringUtils.isNotBlank(joinStr)) {
				randomStr=joinStr+randomStr;
			}
			try {
				flag = redisMapCacheService.pushIfAbsent(key, randomStr, "1", time);
			}catch(Exception e) {
				logger.error("生成唯一编号异常，e:{}",e);
				flag=Boolean.TRUE;
			}
		}
		return randomStr;
	}

}
